#!/bin/bash
# Import all files in the given directory into a Mongodb collection
#

NUM_ARGV=5

if [ $# -lt $NUM_ARGV ]; then
	echo "ERROR Insuffcient arguments. Need $NUM_ARGV but got '$#'" 1>&2
	echo "Usage: ./importall.sh </path/to/taobaoImporter/root/directory> </path/to/data/directory> <database_name> <collection_name> <item_type>" 1>&2
	echo "<item_type> is defined in com.googlecode.ulysses.taobao.Constants.ItemType" 1>&2
	echo "" 1>&2
	exit 1
fi

rootDir=$1; shift
dataDir=$1; shift
# mongodb name and collection
db=$1; shift
coll=$1; shift
itemType=$1; shift

# verify the existence of the directories
if [ ! -e "${rootDir}" -o ! -e "${dataDir}" ]; then
	echo "ERROR lib dir '${rootDir}' or/and data dir '${dataDir}' don't exist. Exit" 1>&2
	echo "Usage: ./importall.sh </path/to/taobaoImporter/root/directory> </path/to/data/directory> <database_name> <collection_name>" 1>&2
	echo "" 1>&2
	exit 1
fi

# trim the tailing '/' character
rootDir=`echo $rootDir | sed -e 's/\/$//'`
dataDir=`echo $dataDir | sed -e 's/\/$//'`

# construct the classpath
classpath=${rootDir}/release/taobao-importer-1.0.0.jar:${rootDir}/lib/commons-cli-1.2.jar:${rootDir}/lib/gson-1.7.1.jar:${rootDir}/lib/mongo-2.6.5.jar:${rootDir}/lib/taobao-sdk-java-20110804.jar:${rootDir}/lib/commons-lang3-3.0.1.jar

# specify the main function
mainClass=com.googlecode.ulysses.taobao.importer.Workman

# import
for i in `ls ${dataDir}/*`
do
	importFile=$i

	echo Start importing file '$i' 1>&2

	echo  java -cp ${classpath} ${mainClass}  -f ${i} -h 127.0.0.1 -p 27017 -t $itemType -d ${db} -c ${coll} 1>&2
	java -cp ${classpath} ${mainClass}  -f ${i} -h 127.0.0.1 -p 27017 -t $itemType -d ${db} -c ${coll}

	echo "" 1>&2
done
